'''
case Type :数据库系统
case Name ：人为修改主备机配置文件，导致主备配置文件不一致，不影响主备数据库运行
Create At: 2020-7-6
owner : @peilinqian
description:1、分别修改主备参数不一致2、查询主备状态3、进行简单数据库sql操作，主备同步功能正常
history：
created： 2020/07/06  created by @peilinqian
'''
import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
from yat.test import macro

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *

from testcase.utils.Common import *


class MyTest(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby1_sh = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case019.py start------------')
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.postgresqlpath = os.path.join(macro.DB_INSTANCE_PATH, macro.DB_PG_CONFIG_NAME)
        self.rootNode = Node(node='PrimaryRoot')
        self.dbUserNode = Node(node='PrimaryDbUser')
        self.standbyrootNode = Node(node='Standby1Root')
        self.standbydbUserNode = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()

    def test_main(self):
        # 主机修改参数值1，备机修改参数值2
        self.newvalue1 = 'shared_buffers = 2GB                    # min 128kB'
        self.newvalue2 = 'shared_buffers = 1GB                    # min 128kB'

        # 主机查找修改之前参数行值
        self.oldvalue1 = self.com.getFileStringRows(self.dbUserNode, self.postgresqlpath, 'shared_buffers = ')
        self.log.info(self.oldvalue1)

        # 主机修改参数行
        msg = self.com.replaceFileContentBySed(self.dbUserNode, self.postgresqlpath, self.oldvalue1, self.newvalue1)
        self.log.info(msg)
        self.assertEqual(msg, '')

        # 备机查找修改之前参数行值
        self.oldvalue2 = self.com.getFileStringRows(self.standbydbUserNode, self.postgresqlpath, 'shared_buffers = ')
        # 备机修改参数行
        msg = self.com.replaceFileContentBySed(self.standbydbUserNode, self.postgresqlpath, self.oldvalue1,
                                               self.newvalue2)
        self.log.info(msg)
        self.assertEqual(msg, '')

        # 查询修改后的值
        value1 = self.com.getFileStringRows(self.dbUserNode, self.postgresqlpath, 'shared_buffers = ')
        self.log.info(value1)
        value2 = self.com.getFileStringRows(self.standbydbUserNode, self.postgresqlpath, 'shared_buffers = ')
        self.log.info(value2)

        # 修改参数完成后，查询主备节点状态正常
        db_status = self.primary_sh.getDbClusterStatus('status')
        self.assertTrue(db_status)

        # 进行简单建表插入操作
        sqlcmd = "drop table if exists test_018;create table test_018(id int,name varchar(20));insert into test_018 values(1,\'ddd\');"
        self.log.info(sqlcmd)
        msg = self.primary_sh.executDbSql(sqlcmd)
        self.log.info(msg)

        # 备机状态正常,成功插入数据
        selectcmd = 'select * from test_018;'
        self.log.info(selectcmd)
        msg = self.primary_sh.executDbSql(selectcmd)
        self.log.info(msg)
        self.assertIn('(1 row)', msg)

    def tearDown(self):
        # 主备参数修改回原值
        msg = self.com.replaceFileContentBySed(self.dbUserNode, self.postgresqlpath, self.newvalue1, self.oldvalue1)
        self.assertEqual(msg, '')

        msg = self.com.replaceFileContentBySed(self.standbydbUserNode, self.postgresqlpath, self.newvalue2,
                                               self.oldvalue1)

        # 清除测试数据
        drop_sql = 'drop table test_018;'
        self.log.info(drop_sql)
        msg = self.primary_sh.executDbSql(drop_sql)
        self.log.info(msg)
        self.assertTrue('DROP TABLE' in msg)
